aprint:
	LINK     A6,#-$128
	MOVEM.L  D4-D7/A3-A4,-(A7)
	;; start of scratch space
	LEA      -$128(A6),A4
#ifdef	SUBPLAY
	LEA      -$108(A6),A3
	LEA      Arg00Link(A6),A0
	ADDQ.W   #4,A0
	MOVE.L   A0,(-$4,A6)
	BRA      aprintGetNextFormatStringCharacter
#else
	;; makes aprint do nothing... real useful...
	BRA	 aprintBypass
#endif
aprintParseFormatStringLoop:	
	CMPI.B   #'%',D6
	BEQ.S    aprintHandleFormatTag
	;; normal character, copy to output
	MOVE.B   D6,(A3)+
	BRA      aprintGetNextFormatStringCharacter
aprintHandleFormatTag:	
	MOVEQ    #$8,D4
	MOVEA.L  Arg00Link(A6),A0
	ADDQ.L   #1,Arg00Link(A6)
	MOVE.B   (A0),D6
	MOVE.B   D6,D0
	;; check for %* (means width is not specified but as an extra arg)
	SUBI.B   #'*',D0
	BNE.S    aprintFormatTagWithNoArgumentWidth
	ADDQ.L   #4,-$4(A6)
	MOVEA.L  -$4(A6),A0
	MOVE.W   -$2(A0),D4
	MOVE.L   Arg00Link(A6),D0
	ADDQ.L   #1,Arg00Link(A6)
	MOVEA.L  D0,A0
	MOVE.B   (A0),D6
aprintFormatTagWithNoArgumentWidth:	
	MOVE.B   D6,D0
	;; %%
	SUBI.B   #'%',D0
	BEQ      aprintDoublePercentCharacter
	;; %S
	SUBI.B   #$2E,D0
	BEQ.S    aprintString
	;; %X
	SUBQ.B   #5,D0
	BEQ.S    aprintUnsignedHexadecimalInteger
	;; %d
	SUBI.B   #$C,D0
	BEQ.S    aprintSignedDecimalInteger
	;; %s
	SUBI.B   #$F,D0
	BEQ.S    aprintString
	;; %x
	SUBQ.B   #5,D0
	BEQ.S    aprintUnsignedHexadecimalInteger
	;; unsupported format tag, ignore and process next character
	BRA      aprintGetNextFormatStringCharacter
aprintSignedDecimalInteger:	
	ADDQ.L   #4,-$4(A6)
	MOVEA.L  -$4(A6),A0
	MOVE.L   -$4(A0),D5
	MOVE.L   A4,-(A7)
	EXT.L    D4
	MOVE.L   D4,-(A7)
	MOVE.L   D5,-(A7)
	JSR      NumStr(PC)
	CLR.W    D7
	LEA      $C(A7),A7
	BRA.S    Label1534
Label152C:	
	MOVE.B   $0(A4,D7.W),(A3)+
	MOVE.W   D7,D0
	ADDQ.W   #1,D7
Label1534:	
	TST.B    $0(A4,D7.W)
	BNE.S    Label152C
	BRA.S    aprintGetNextFormatStringCharacter
aprintUnsignedHexadecimalInteger:	
	ADDQ.L   #4,-$4(A6)
	MOVEA.L  -$4(A6),A0
	MOVE.L   -$4(A0),D5
	MOVE.L   A4,-(A7)
	EXT.L    D4
	MOVE.L   D4,-(A7)
	MOVE.L   D5,-(A7)
	JSR      HexStr(PC)
	CLR.W    D7
	LEA      $C(A7),A7
	BRA.S    Label1564
Label155C:	
	MOVE.B   $0(A4,D7.W),(A3)+
	MOVE.W   D7,D0
	ADDQ.W   #1,D7
Label1564:	
	TST.B    $0(A4,D7.W)
	BNE.S    Label155C
	BRA.S    aprintGetNextFormatStringCharacter
aprintString:
	ADDQ.L   #4,-$4(A6)
	MOVEA.L  -$4(A6),A0
	MOVE.L   -$4(A0),-$8(A6)
	BRA.S    Label1588
Label157C:	
	MOVE.L   -$8(A6),D0
	ADDQ.L   #1,-$8(A6)
	MOVEA.L  D0,A0
	MOVE.B   (A0),(A3)+
Label1588:	
	MOVEA.L  -$8(A6),A0
	;; check for null character (string terminator)
	TST.B    (A0)
	BNE.S    Label157C
	BRA.S    aprintGetNextFormatStringCharacter
aprintDoublePercentCharacter:	
	;; normal character, copy to output
	MOVE.B   D6,(A3)+
aprintGetNextFormatStringCharacter:	
	MOVE.L   Arg00Link(A6),D0
	ADDQ.L   #1,Arg00Link(A6)
	MOVEA.L  D0,A0
	MOVE.B   (A0),D6
	;; check for null character (string terminator)
	BNE      aprintParseFormatStringLoop
	CLR.B    (A3)+
	PEA      -$108(A6)
	JSR      DrawText(PC)
	ADDQ.W   #4,A7
aprintBypass:	
	MOVEM.L  -$140(A6),D4-D7/A3-A4
	UNLK     A6
	RTS

	dc.b	$86,"aprint",$00,$00,$00
	even
